package ext.csc.component.numbergen.utilities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.OrganizationServicesHelper;
import wt.org.WTGroup;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.util.WTException;

public class RemoteUtility implements RemoteAccess, Serializable{
	private static final long serialVersionUID = -103129796066207963L;

	public static List<WTGroup> getUserGroups(WTUser user){
		try {
			if (!RemoteMethodServer.ServerFlag) {
				return (List<WTGroup>) RemoteMethodServer.getDefault().invoke("getUserGroupsRemote", RemoteUtility.class.getName(), null,
						new Class[] {WTUser.class},
						new Object[] {user});
			} else {
				return getUserGroupsRemote(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
    public static List<WTGroup> getUserGroupsRemote(WTUser user) throws WTException{
        List<WTGroup> userGroupList = new ArrayList<WTGroup>();
        boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
        try{
                 Enumeration em = OrganizationServicesHelper.manager.parentGroups(user);
                 while(em.hasMoreElements()){
                          WTGroup group = (WTGroup)((WTPrincipalReference)em.nextElement()).getObject();
                          userGroupList.add(group);
                 }
        }catch(WTException e){
                 e.printStackTrace();
        }finally{
                 wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
        }
        return userGroupList;
    }
}
